using System;
using System.Windows.Forms;
using CSettingsUI;


namespace CSettingsUI
{
   

	/// <summary>
	/// IPropertyConverterControl implementation for CheckBox
	/// </summary>
	public class CheckBoxOption : CheckBox, IPropertyConverterControl
	{
		public CheckBoxOption()
		{
			//
			// TODO: Add constructor logic here
			//
		}
	
		#region IPropertyConverterControl Members

		public object Value
		{
			get
			{
				// TODO:  Add TextBoxOption.Value getter implementation
				return base.Checked;
			}
			set
			{
				if(value is bool) base.Checked= (bool) value;
			}
		}

		#endregion
	}
	/// <summary>
	/// IPropertyConverterControl implementation for NumericUpDown
	/// </summary>
	public class NumericUpDownOption : NumericUpDown, IPropertyConverterControl
	{
		public NumericUpDownOption ()
		{			
		}
	
		#region IPropertyConverterControl Members
		private bool isInt;
		new public object Value
		{
			get
			{
				// TODO:  Add TextBoxOption.Value getter implementation
				if(isInt)
				{
					return Convert.ToInt32(base.Value);
				} 
				else
				{
					return base.Value;
				}
			}
			set
			{
				isInt = value.GetType().Equals(typeof(int));	
				if(isInt)
				{
					this.Maximum = int.MaxValue;
					this.Minimum = int.MinValue;
					this.DecimalPlaces = 0;
				}
				else
				{
					this.Maximum = Decimal.MaxValue;
					this.Minimum = Decimal.MinValue;
					this.DecimalPlaces = 2;
				}
				
				base.Value = Convert.ToDecimal(value);
			}
		}

		#endregion
	}
	/// <summary>
	/// IPropertyConverterControl implementation for DateTimePicker
	/// </summary>
	public class DateTimePickerOption : DateTimePicker, IPropertyConverterControl
	{
		public DateTimePickerOption ()
		{
            this.MaxDate = new DateTime(3000, 1, 1);
            this.MinDate = new DateTime(1, 1, 1);
		}
	
		#region IPropertyConverterControl Members

		object IPropertyConverterControl.Value
		{
			get
			{
				// TODO:  Add TextBoxOption.Value getter implementation
				return (DateTime) base.Value;
			}
			set
			{
				if(value is DateTime) base.Value= (DateTime) value;
			}
		}

		#endregion
	}
	/// <summary>
	/// IPropertyConverterControl implementation for ComboBox
	/// </summary>
	public class ComboBoxOption : ComboBox, IPropertyConverterControl
	{
		public ComboBoxOption ()
		{
			this.DropDownStyle = ComboBoxStyle.DropDownList;
            
		}
	
		#region IPropertyConverterControl Members
		private Type currentType;
		public object Value
		{
			get
			{
				// TODO:  Add TextBoxOption.Value getter implementation
				return this.SelectedItem;
			}
			set
			{
				if(value==null) return;
				//
				if(currentType != value.GetType())
				{
					currentType = value.GetType();
					this.DataSource = Enum.GetValues(value.GetType());
				}
				//
				this.SelectedItem = value;
			}
		}

		#endregion
	}
	/// <summary>
	/// IPropertyConverterControl implementation for CheckedListBox
	/// </summary>
	public class CheckedListBoxOption : CheckedListBox, IPropertyConverterControl
	{
		public CheckedListBoxOption  ()
		{	
			this.Width = 200;
		}
	
		#region IPropertyConverterControl Members
		private Type currentType;
		public object Value
		{
			get
			{
				System.Text.StringBuilder s = new System.Text.StringBuilder(this.CheckedItems.Count);
				foreach(object item in this.CheckedItems )
				{				
					if(s.Length>0)
					{
						s.Append(",");
					}
                    s.Append(item.ToString());					
				}
				//
				if(s.Length==0)
				{					
					return Activator.CreateInstance(currentType.GetType());
				}
				return Enum.Parse(currentType, s.ToString());
                //int  ret = 0;
                //foreach(int i in this.CheckedItems )
                //{
                //    if(ret==0)
                //    {
                //        ret = i;
                //    }
                //    else
                //    {
                //        ret = ret | i;
                //    }
					
                //}
                //return Enum.ToObject(currentType,ret);				
			}
			set
			{
				if(value==null) return;
				//
				if(currentType != value.GetType())
				{
					currentType = value.GetType();					
					this.DataSource = Enum.GetValues(value.GetType());
				}
				for(int i = 0;i<this.Items.Count;i++)
				{					
					this.SetItemChecked(i,((Convert.ToInt32( Enum.Format(currentType,value,"d")) & (int) this.Items[i]) == (int) this.Items[i]));					
				}
			}
		}

		#endregion
	}

}
